<?php

namespace App\Http\Controllers\Activity\Web;

use App\Enums\WorkOrder\OrderEnum;
use App\Http\Controllers\BaseController;
use App\Http\Validate\Activity\ShopOrderActivityValidate;
use App\Jobs\Export\Export;
use App\Models\Activity\ShopOrderActivityModel;
use App\Service\Activity\ShopOrderActivityService;
use App\Service\OpenShop\Feishu\ConfigService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopOrderActivityController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function list(Request $request): JsonResponse
    {
        return $this->success(
            ShopOrderActivityService::list(
                ShopOrderActivityValidate::listValidate($request->all())
            )
        );
    }

    /**
     * 新增活动
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function insert(Request $request): JsonResponse
    {
        $params = ShopOrderActivityValidate::insertValidate($request->all());
        ShopOrderActivityService::insert($request->user()->id, $params);
        return $this->success();
    }

    /**
     * 编辑
     *
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function update(int $id, Request $request): JsonResponse
    {
        ShopOrderActivityService::update(
            $request->user()->id,
            ShopOrderActivityValidate::updateValidate($request->merge(["id" => $id])->all())
        );
        return $this->success();
    }

    /**
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function status(int $id, Request $request): JsonResponse
    {
        $params = ShopOrderActivityValidate::statusValidate($request->merge(["id" => $id])->all());
        ShopOrderActivityService::status($request->user()->id, $params);
        return $this->success();
    }

    /**
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function detail(int $id, Request $request): JsonResponse
    {
        ShopOrderActivityValidate::detailValidate($request->merge(["id" => $id])->all());
        return $this->success(ShopOrderActivityService::detail($id));
    }

    /**
     * 结束
     *
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function finish(int $id, Request $request): JsonResponse
    {
        ShopOrderActivityValidate::finishValidate($request->merge(["id" => $id])->all());
        ShopOrderActivityService::finish($request->user()->id, $id);
        return $this->success();
    }

    /**
     * 活动数据统计
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function dataStatistics(Request $request): JsonResponse
    {
        return $this->success(
            ShopOrderActivityService::dataStatistics(
                ShopOrderActivityValidate::dataStatisticsValidate($request->all())
            )
        );
    }

    /**
     * 活动数据统计
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function dataStatisticsShopList(Request $request): JsonResponse
    {

        $params = ShopOrderActivityValidate::dataStatisticsShopListValidate($request->all());
        if ($params->export) {
            $fileName = vsprintf("%s_%s_%s", [
                ShopOrderActivityModel::query()->where("id", $params->id)->first()->name,
                ShopOrderActivityModel::BUSINESS_TYPE_TEXT[$params->businessType] ?? '未定义的类型',
                date("Y_m_d_H_i_s", time())
            ]);
            dispatch(
                (new Export(
                    $fileName, Export::TYPE_SHOP_ORDER_ACTIVITY_SHOP_LIST, [
                        'data'   => (array)$params,
                        'userId' => $request->user()->id,
                    ]
                ))
            )->onQueue(Export::QUEUE_NAME);
            return $this->success([]);
        }

        return $this->success(ShopOrderActivityService::dataStatisticsShopList($params));
    }

    /**
     * 手动推送 web 页面
     *
     * @param Request $request
     * @return Application|Factory|View
     * @throws \Exception
     */
    public function manualSendNotifyWeb(Request $request)
    {
        $list = ConfigService::config('manual', 'orderActivityList')['orderActivityList'];
        return view('manual.orderActivity.index', ['data' => $list]);
    }

    /**
     * 推送 Call
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function manualSendNotify(Request $request): JsonResponse
    {
        $activityId     = $request->get('activityId');
        $notificationId = $request->get('notificationId');

        if (isPre()) {
            return $this->fail(0, '预发环境不发送');
        }

        Artisan::call(
            "activity:send-activity-feedback",
            [
                '--username'       => $request->user()->username,
                '--activityId'     => $activityId,
                '--notificationId' => $notificationId
            ]
        );
        return $this->success([]);
    }
}
